
const connections=require("../app/databases")

class CommentService{
    //评论
    async createComment(content,momentid,id){
        const statement =`insert into comment (content,moment_id,user_id) values(?,?,?);`
        console.log(connections)
        const result=await connections.execute(statement,[content,momentid,id])
        return result;
    }
    //对评论做出评论
    async replyComment(content,momentid,id,commentid){
        const statement =`insert into comment (content,moment_id,user_id,comment_id) values(?,?,?,?);`
        console.log(content+momentid+id+commentid)
       const result =  await connections.execute(statement,[content,momentid,id,commentid]);
        console.log("a")
        return result;
    }
    //修改评论
    async updateComment(content,commentid){
        const  statement=`update comment set content=? where id=?`
        const result=await connections.execute(statement,[content,commentid])
        return result;
    }
    //删除评论
    async removeComment(commentid){
        const statement=`delete from comment where id=?`
        console.log(commentid)
        const result=await connections.execute(statement,[commentid])
        return result;
    }
    //获取评论
    async getCommentList(momentid){
        const statement=`select  c.id id,c.content content,c.createAt createTime,c.updateAt updateTime,json_object("id",u.id,"name",u.name) as autor from comment c   LEFT JOIN user u on c.user_id=u.id   where c.moment_id=?;`
        const result=await connections.execute(statement,[momentid])
       
        return result[0]
    }
      //查看详情
   async getDetailComment(momentid){
    const statement=`select m.content content,m.createAt createTime,m.updateAt updateTime,
    json_object("id",u.id,"name",u.name) as autor,
   JSON_ARRAYAGG(
       json_object("id",c.id,"content",c.content,"createTime",c.createAt,"updateTime",c.updateAt,
       "user",json_object('id',us.id,'name',us.name))
   ) as comment
    from moment m
   LEFT JOIN user u on  m.user_id=u.id
   LEFT JOIN COMMENT c on m.id=c.moment_id
   LEFT JOIN user us on c.user_id=us.id
   where m.id=?`
   try {
        const result=await connections.execute(statement,[momentid])
        return result[0];
   } catch (error) {
        console.log(error)
   }
}
}
module.exports=new CommentService();
